<template>
  <div class="all">
    <div class="top">
      <el-row>
        <el-form ref="form" inline label-position="left">
          <el-col :span="3">
            <el-form-item label="编 码：">
              <el-input v-model="query.coding" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="名 称：">
              <el-input v-model="query.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="类 型：">
              <SelfSelect v-model="query.type" :showList="targetType" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-button style="margin-top: 38%" icon="el-icon-search" type="primary" @click="index">查找</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-row class="btns">
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" @click="delBtn">删除</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="refresh">刷新</el-button>
      </el-row>
      <span class="total">总数：{{ total }}</span>
    </div>
    <div class="main">
      <div class="table">
        <el-table ref="multipleTable" v-loading="loading" border tooltip-effect="dark" stripe width="100%" :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column label="序号" type="index" align="center" width="50" />
          <el-table-column type="selection" align="center" width="55" />
          <el-table-column prop="coding" label="编号" align="center" />
          <el-table-column prop="name" label="名称" align="center" />
          <el-table-column prop="type" label="类型" align="center" />
          <el-table-column prop="sort" label="排序" align="center" />
          <el-table-column label="操作" fixed="right" width="180" align="center">
            <template scope="{row}">
              <el-button type="primary" @click="goDetail(row.id)">编辑</el-button>
              <el-button type="danger" @click="handleDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination class="page" background :page-size="query.limit" align="center" layout="total,prev,pager,next,jumper" :total="this.total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
      </div>
    </div>
  </div>
</template>

<script>
import API from '@/api/charts/targetCategory'
import SelfSelect from '@/components/select-o'
import { targetType } from '@/utils/option'
import { mapGetters } from 'vuex'
export default {
  inject: ['goUrl', 'reload'],
  components: {
    SelfSelect
  },
  data() {
    return {
      dataList: [],
      total: 0,
      loading: true,
      targetType,
      query: {
        limit: 15,
        page: 1
      },
      multipleSelection: []
    }
  },
  created() {
    const query = JSON.parse(sessionStorage.getItem('query'))
    if (query) {
      this.query = query
      sessionStorage.removeItem('query')
    }
    this.index()
    window.addEventListener('keydown', this.handKeyCode, true)
  },
  computed: {
    ...mapGetters(['roles'])
  },
  methods: {
    index() {
      this.loading = true
      API.getList(this.query).then(res => {
        this.dataList = res.data.data
        this.total = res.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    handKeyCode(e) {
      if (e.keyCode === 13 || e.keyCode === 32) { // 回车搜索
        this.index()
      }
    },
    setStorage() {
      sessionStorage.setItem('query', JSON.stringify(this.query))
    },
    goDetail(id) {
      this.setStorage()
      const url = `/charts/targetCategory/edit/${id}`
      this.goUrl(url)
    },
    handleAdd() {
      this.setStorage()
      this.goUrl('/charts/targetCategory/add')
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete(id).then(res => {
          if (res.data.error_code !== 1) {
            this.$message.error(res.data.message)
          } else {
            this.setStorage()
            this.$message.success('删除成功')
            this.index()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delBtn() {
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const idArray = []
        this.multipleSelection.forEach(item => {
          idArray.push(item.id)
        })
        const id = idArray.join(',')
        this.handleDel(id)
      } else {
        this.$message('请先选择，然后再删除')
      }
    },
    refresh() {
      this.reload()
    }
  }
}
</script>
